#ifndef __H_MVP_TRANSPORT_PPS_RECEIVER_H__
#define __H_MVP_TRANSPORT_PPS_RECEIVER_H__

#include "transport/receiver/receiver.h"
#include "transport/pps/pps_proxy.h"

namespace motovis {
namespace transport {

template <typename MessageT>
class PpsReceiver : public Receiver<MessageT> {
public:
    using MessagePtr = std::shared_ptr<MessageT>;
    using MessageListener = std::function<void(const MessagePtr &msg)>;
    using Callback = std::function<void(const pps::ReceiveData *)>;

    void addListener(const std::string &channelName, const MessageListener &msgListener) override;
    void onMessage(const pps::ReceiveData *r_buf);

private:
    MessageListener m_listener;
};

template <typename MessageT>
void PpsReceiver<MessageT>::addListener(const std::string &channelName, const MessageListener &msg_listener)
{
    m_listener = msg_listener;
    Callback cb = std::bind(&PpsReceiver<MessageT>::onMessage, this, std::placeholders::_1);
    PpsProxy::Instance()->subscribe(channelName, cb);
}

template <typename MessageT>
void PpsReceiver<MessageT>::onMessage(const pps::ReceiveData *r_buf)
{
    m_listener(std::make_shared<MessageT>(*(MessageT*)(r_buf->data)));
}

} // namespace transport
} // namespace motovis

#endif // __H_MVP_TRANSPORT_PPS_RECEIVER_H__
